package com.levi.springextension;

import com.TestRegistryPostProcessor;
import org.junit.Test;
import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.*;
import org.springframework.stereotype.Component;

/**  2
 * @author shentong
 * @since 2023/1/12 11:49
 * 这个接口在读取项目中的beanDefinition之后执行，提供一个补充的扩展点,可以再向容器中添加一些bean定义
 */
@Component
public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        System.out.println("之前的bean数量: "+registry.getBeanDefinitionCount());
        AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(TestRegistryPostProcessor.class).getBeanDefinition();
        beanDefinition.setBeanClass(TestRegistryPostProcessor.class);
        registry.registerBeanDefinition("testRegistryPostProcessor", beanDefinition);
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println("之后的bean数量"+beanFactory.getBeanDefinitionCount());
    }
}
